package com.bjpowernode.shop.domain;

import com.alibaba.nacos.shaded.com.google.protobuf.StringValue;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;

/**
 * 系统用户
 *
 * @TableName sys_user
 */
@TableName(value = "sys_user")
@Data
public class SysUser implements Serializable, UserDetails {
    /**
     *
     */
    @TableId(value = "user_id", type = IdType.AUTO)
    private Long userId;

    /**
     * 用户名
     */
    @TableField(value = "username")
    private String username;

    /**
     * 密码
     */
    @TableField(value = "password")
    private String password;

    /**
     * 邮箱
     */
    @TableField(value = "email")
    private String email;

    /**
     * 手机号
     */
    @TableField(value = "mobile")
    private String mobile;

    /**
     * 状态  0：禁用   1：正常
     */
    @TableField(value = "status")
    private Integer status;

    /**
     * 创建者ID
     */
    @TableField(value = "create_user_id")
    private Long createUserId;

    /**
     * 创建时间
     */
    @TableField(value = "create_time")
    private Date createTime;

    /**
     * 用户所在的商城Id
     */
    @TableField(value = "shop_id")
    private Long shopId;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @TableField(exist = false)
    private List<String> authList;

    @Override
    //将权限的原集合List，转换为框架所需的权限集合List<GrantedAuthority>
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
        //遍历权限列表数据
        authList.forEach(
                auth -> {
                    //当前的权限，可能包含一个或多个，按照字符串来进行匹配，包含逗号的，代表有多个权限
                    if (StringUtils.hasText(auth)) {
                        //多个权限
                        if (auth.contains(",")) {
                            //拆分字符串
                            String[] auths = auth.split(",");
                            //遍历封装
                            for (String au : auths) {
                                grantedAuthorityList.add(
                                        new SimpleGrantedAuthority(au)
                                );
                            }
                        } else {
                            //单个权限
                            grantedAuthorityList.add(
                                    new SimpleGrantedAuthority(auth)
                            );
                        }
                    }
                }
        );
        return grantedAuthorityList;
    }

    public String getUsername() {
        return String.valueOf(this.userId);
    }

    public String getPassword() {
        return this.password;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }
}